package com.xidian.zhiancloud.controller.myyb;

import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.enums.UserResponseEnums;
import com.xidian.zhiancloud.model.SysCompany;
import com.xidian.zhiancloud.model.SysUserAbandon;
import com.xidian.zhiancloud.service.myyb.ICompanyService;
import com.xidian.zhiancloud.service.myyb.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 系统设置-登录
 */
@RestController
@RequestMapping(value = "/sys")
public class SysPageController extends BaseController {

    private final static Logger logger = LoggerFactory.
            getLogger(SysPageController.class);

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ICompanyService companyService;

    @Autowired
    HttpServletRequest request;

    /**
     * 登录
     *
     * @param username  用户名称
     * @param passwords 密码
     * @return 返回ResultDTO信息
     */
    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResultDTO login(@RequestParam("username") String username,
                           @RequestParam("passwords") String passwords
                           ) {
        SysUserAbandon user = new SysUserAbandon();
        try{
            if(passwords == null || passwords.length() < 6){
                return ResultDTO.errorOf(UserResponseEnums.PASSWORD_LESS_THAN_SIX);
            }
            user = userService.selectByUserNameAndPassWords(username, passwords);
            if (user == null) {
                return ResultDTO.errorOf(UserResponseEnums.USER_NOT_FOUND);
            }
            //saveSessionUser(user);//将用户信息放入session
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return ResultDTO.successOf(user,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 注册新用户
     * @param sysCompany 公司名称
     * @param sysUserAbandon 用户信息
     * param passwords 输入密码
     * param passwords2 确认密码
     * @return 返回ResultDTO信息
     */
    @ResponseBody
    @RequestMapping(value ="/uregister", method = RequestMethod.POST)
    public ResultDTO addUser(SysCompany sysCompany, SysUserAbandon sysUserAbandon,
                             @RequestParam("passwords") String passwords,
                             @RequestParam("passwords2") String passwords2) {
        try{
            if (!passwords.equals(passwords2)) {//两次密码比较是否相同
                return ResultDTO.errorOf(UserResponseEnums.PASSWORDS_ARE_NOTTHE_SAME);
            } else {
                List<SysCompany> companyList = companyService.selectByCompanyName(
                        sysCompany.getName());
                if (companyList.size() == 0) {
                    companyService.save(sysCompany);
                }
                List<SysCompany> companyLists = companyService.selectByCompanyName(
                        sysCompany.getName());
                SysCompany company1 = new SysCompany();
                for (SysCompany company : companyLists){
                    company1.setId(company.getId());
                }
                //激活账号时判断手机号
                List<SysUserAbandon> phoneNumList = userService.selectByPhoneNum(sysUserAbandon.getPhoneNum());
                //判断是否有超级管理员存在
                List<SysUserAbandon> userList = userService.selectByCompanyAndRoleId(
                        company1.getId(), Long.valueOf("1"));
                if(phoneNumList.size() == 0){
                    if (userList.size() == 0) {//查看user表有没有超级管理员
                        sysUserAbandon.setRoleId(Integer.valueOf("1"));//超级管理员为 1
                    }
                    sysUserAbandon.setRoleId(Integer.valueOf("2"));//普通管理员为 2
                    if (StringUtils.isEmpty(sysUserAbandon.getOrganizationId())) {
                        sysUserAbandon.setOrganizationId(Long.valueOf("1"));
                    }
                    if (StringUtils.isEmpty(sysUserAbandon.getDepartmentId())) {
                        sysUserAbandon.setDepartmentId(Long.valueOf("1"));
                    }
                    if (StringUtils.isEmpty(sysUserAbandon.getPositionId())) {
                        sysUserAbandon.setPositionId(Long.valueOf("1"));
                    }
                    if (StringUtils.isEmpty(sysUserAbandon.getPostId())) {
                        sysUserAbandon.setPostId(Long.valueOf("1"));
                    }
                    sysUserAbandon.setCompanyId(company1.getId());//添加的公司ID
                    userService.save(sysUserAbandon);
                }else{
                    return ResultDTO.errorOf(UserResponseEnums.PHONE_NUMBER_ALREADY_EXISTS);
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return ResultDTO.successOf(UserResponseEnums
                .ACCOUNT_ACTIVATED_PLEASE_LOGIN_AGAIN);
    }

    /**
     * 退出登录
     * *
     * @param session 登录信息
     * @return 返回ResultDTO信息
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public ResultDTO logout(HttpSession session) {
         try{
             removeSession();
            System.out.println("退出登录");
         } catch (Exception e) {
            logger.error(e.getMessage());
         }
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }


}
